package com.koicarp.university.graduate.auth.manager;

import com.koicarp.university.graudate.common.dao.SysMenuDao;
import com.koicarp.university.graudate.common.dao.SysUserDao;
import com.koicarp.university.graudate.common.dto.MenuVueDto;
import com.koicarp.university.graudate.common.dto.Meta;
import com.koicarp.university.graudate.common.dto.SysUserDto;
import com.koicarp.university.graudate.common.entity.SysMenu;
import com.koicarp.university.graudate.common.utils.UniversityTreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther liutao
 * @Date 2020-08-17 12:00:19
 */
@Service
public class UserManager {
    @Autowired(required = true)
    private SysUserDao userMapper;
    @Autowired(required = true)
    private SysMenuDao menuMapper;

    public SysUserDto findByUserIdOrTel(String condition){
        return userMapper.findByUserIdOrTel(condition);
    }

    public String findUserPermissions(String username){
        List<SysMenu> userPermissions =menuMapper.findUserPermissions(username);
        List<String> perms=new ArrayList<>();
        for (SysMenu m:userPermissions){
            perms.add(m.getPerms());
        }
        return StringUtils.join(perms,",");
    }

    public List<MenuVueDto> findMenuByRole(String[] roles){
        List<MenuVueDto> list=new ArrayList<>();
        List<SysMenu> listMenu = menuMapper.selectMenuByRole(roles);
        for (SysMenu sysMenu:listMenu){
            MenuVueDto menuVueDto=new MenuVueDto();
            menuVueDto.setRootId(Integer.valueOf(sysMenu.getParentId()));
            menuVueDto.setRedirect(sysMenu.getRedirect());
            menuVueDto.setPath(sysMenu.getPath());
            menuVueDto.setComponent(sysMenu.getComponent());
            menuVueDto.setName(sysMenu.getMenuName());
            menuVueDto.setMenuId(Integer.parseInt(sysMenu.getMenuId()));
            menuVueDto.setHidden(sysMenu.getHidden());
            menuVueDto.setNoCache(sysMenu.getNoCache());
            Meta meta=new Meta();
            meta.setIcon(sysMenu.getIcon());
//            meta.setRoles(roles);
            meta.setTitle(sysMenu.getMenuName());
            menuVueDto.setMeta(meta);
            list.add(menuVueDto);
        }
        List<MenuVueDto> tree = UniversityTreeUtil.buildTree(list);
        return tree;
    }
}
